let instance
let player
var lastclickmill=0
var thisclickmill=0
var touchmovenum =0



/**
 * 全局状态管理器
 */
 
export default class DataBus {
  constructor() {
    
	if ( instance )
      return instance

    instance = this
    this.GAME_INDEX = 0         //init from 0
	
    //this.player=null
    //this.reset()
  }
  
  setplayer(newplayer){
	  player=newplayer
  }

  reset() {
    this.frame      = 0
    this.score      = 0
    //this.bullets    = []
    //this.enemys     = []
    //this.animations = []
    this.gameOver   = false
	this.gamestart  = false
	this.gameALLOver =false
	this.gamepause =false
	//this.breakflag=false //如果玩家被吃,breakflag=true
	this.MAP_OFFSETX=10 //x offset
    this.MAP_OFFSETY=-80 //y offset
	this.NPC_NUM=4   // npc number olval:4
	this.GAME_LASTINDEX=10 //游戏总关数
    this.mapdataflag=false //全局地图变量
	this.mapdata=null
	this.score=0
	this.mapmoving=false
	this.movingdirection=0 //1:up,-1:down
	this.size= 16 //地图单元的长度
	this._COS = [1,0,-1,0]
    this._SIN = [0,1,0,-1]
  

	
	 wx.onTouchStart(function (e) {
       player.x0 = e.changedTouches[0].clientX
       player.y0 = e.changedTouches[0].clientY
       player.startId = e.changedTouches[0].identifier
		  
		  
		  /*
		  var clickDate = new Date();
	
	      //double click: 120-300ms

		  thisclickmill=clickDate.getTime()
		  clickDate=null
		  if (thisclickmill-lastclickmill>400){
		     lastclickmill=thisclickmill //记录第一次点击
			 touchmovenum=0
	      }else{
		     if (Math.abs(lastclickmill-thisclickmill)<50){ //(连续两次点击小于50毫秒，非人为)
    			 lastclickmill=0//thisclickmill
				 touchmovenum=0
				 return 
			 }
			 
			 if(touchmovenum<=5 && instance.gamestart){ //防止真机上双击的时候，混入touchmove事件 ,关卡(3,2,1)显示时不许暂停
			   if (!instance.gamepause)
		         instance.gamepause=true
		       else
			     instance.gamepause=false
		       
			 }
			 
			 touchmovenum=0 //清空touchmove 次数
			 //lastclickmill=0
		  }*/
    })
    
	/*获取结束坐标和id*/
    
	wx.onTouchEnd(function (e) {
    
	  if (!instance.gamepause){ 
      
	     player.xt = e.changedTouches[0].clientX
         player.yt = e.changedTouches[0].clientY
         player.endId = e.changedTouches[0].identifier
        /*判断并回调*/
         player._callorient()
		 
	  }
    })
	
	/*
	wx.onTouchMove(function (e) {
       touchmovenum++;
	})*/
	
  }

  setnpcitems(items){
    this.npcitems=items
  }
  
  /*
  setbreakflag(flag){
    this.breakflag=flag
  }*/
  
  resetmapoffset(){
    this.MAP_OFFSETX=10 //x offset
    this.MAP_OFFSETY=-80 //y offset
  }
  
  
  /*
  getdoubleckick(){
    return {lt:lastclickmill,
          it:thisclickmill,
		  ot:thisclickmill-lastclickmill,
		  mvnum:touchmovenum
    }
  }*/
}
